package xyz.chenyongpeng.ticketconsumerdubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.orderconsumerdubbo.service.GoodService;
import xyz.chenyongpeng.ticketconsumerdubbo.service.TicketService;
import xyz.chenyongpeng.ticketconsumerdubbo.vo.DayTicket;
import xyz.chenyongpeng.ticketconsumerdubbo.vo.MonthTicket;
import xyz.chenyongpeng.to.GoodsTo;
import xyz.chenyongpeng.utils.R;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/Ticket")
public class TicketController {

    @Autowired
    private GoodService goodService;

    @Autowired
    private TicketService ticketService;
    /**
     * 按天查询票务信息，调用票务通接口。
     * @param goods_id
     * @param date
     * @return
     */
    @PostMapping("/queryDay")
    public R queryDay(@RequestParam(name = "goods_id",required = true) String goods_id,
                      @RequestParam(name = "date",required = true) String date){
        Map<String,Object> result = new HashMap<>();
        DayTicket dayTicket = new DayTicket();
        dayTicket.setStarttime("6:00");
        dayTicket.setStarttime("18:00");
        dayTicket.setStorage(new Random().nextInt(999)+100);
        dayTicket.setRemain(new Random().nextInt(99)+1);

        result.put("data", dayTicket);
        return R.ok(result);
    }


    /**
     * 按月查询票务信息，从今天开始，调用票务通接口。
     * @param goods_id
     * @return
     */
    @PostMapping("/queryDay")
    public R queryDay(@RequestParam(name = "goods_id",required = true) String goods_id){
        if (StringUtils.isEmpty(goods_id)){
            return R.error(500, "参数不全");
        }
        GoodsTo goodsTo = goodService.getOne(goods_id);
        if (goodsTo == null){
            return R.error(404, "没有这个商品");
        }

        Map<String,Object> result = new HashMap<>();
        Map<String,MonthTicket> monthTicketMap = new HashMap<>();
        for (int i = 0; i < 30; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR)+i);
            Date today = calendar.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String day = format.format(today);
            MonthTicket monthTicket = new MonthTicket(99999,99999);
            monthTicketMap.put(day, monthTicket);
        }
        result.put("data", monthTicketMap);
        return R.ok(result);
    }

    @PostMapping("yyStock")
    public R yyStock(){
        Map<String,Object> tickets = ticketService.getList();
        Map<String,Object> result = new HashMap<>();
        result.put("data", tickets);
        return R.ok(result);
    }
}
